/**
 * 
 */
package edu.gatech.oad.yuno;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * @author ryanfork
 *
 */
public class AddCategoryActivity extends Activity implements View.OnClickListener{
	private Button addCategory;
	private EditText editName;
	private Spinner editColor;
	
	private Category cat;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_category);
		editName= (EditText) findViewById(R.id.editCategoryName);
		editColor = (Spinner) findViewById(R.id.colorSpinner);
		addCategory = (Button) findViewById(R.id.addCatButton);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.label_color_array, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		editColor.setAdapter(adapter);
		addCategory.setOnClickListener(this);
	}

	public void onClick(View v) {
		if (v == addCategory) {
			cat = new Category();
			SharedPreferences prefs = getSharedPreferences("YUNoDos", MODE_PRIVATE);
			cat.setUser(prefs.getLong("logged_in_user_id", 0));
			cat.setName(editName.getText().toString());
			String color = editColor.getSelectedItem().toString();
			if (color.equals("Red")) {
				cat.setColor(Color.RED);
			} else if (color.equals("Green")) {
				cat.setColor(Color.GREEN);
			} else if (color.equals("Blue")) {
				cat.setColor(Color.BLUE);
			} else if (color.equals("Orange")) {
				cat.setColor(Color.rgb(255, 100, 0));
			} else if (color.equals("Purple")) {
				cat.setColor(Color.rgb(190, 100, 255));
			} else if (color.equals("Yellow")) {
				cat.setColor(Color.YELLOW);
			} else {
				cat.setColor(Color.WHITE);
			}

			try {
				cat.validate();
				cat.save();
				finish();
				Toast.makeText(getApplicationContext(),editName.getText().toString() + " saved.",
						Toast.LENGTH_SHORT).show();
			}
			catch (ValidationException e) {
				Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
			}
		}
	}
}